/*
 * Powered By [pkuWx]
 * Web Site: http://www.wuxi.ss.pku.edu.cn
 * Since 2015 - 2015
 */

package cn.edu.pku.wxlib.user.dao;

import java.util.HashMap;
import java.util.Map;

import javacommon.base.BaseIbatis3Dao;
import javacommon.page.Page;

import org.springframework.util.Assert;

import cn.edu.pku.wxlib.user.model.User;
import cn.edu.pku.wxlib.user.vo.query.UserQuery;
import cn.edu.pku.wxlib.vo.response.StudentUser;
import cn.edu.pku.wxlib.vo.response.TeacherUser;
/**
 * @version 1.0
 * @since 1.0
 */
import org.springframework.stereotype.Repository;


@Repository
public class UserDao extends BaseIbatis3Dao<User,UserQuery,java.lang.Integer>{
	
	@Override
	public String getIbatisMapperNamesapce() {
		return "User";
	}
	
	public void saveOrUpdate(User entity) {
		if(entity.getUserId() == null) 
			save(entity);
		else 
			update(entity);
	}
	
	public Page findPage(UserQuery query) {
		return pageQuery("User.findPage",query);
	}
	
	//改变用户状态
	public void changeState(final Object... params) {
		getSqlSessionTemplate().update("User.changeState", map(params));
	}
	
	protected Map<?, ?> map(final Object... datas) {
		Assert.isTrue(datas.length%2==0, "参数必须为偶数个");
		final Map<Object, Object> map = new HashMap<Object, Object>();
		for (int i = 0; i < datas.length; i += 2) {
			map.put(datas[i], datas[i+1]);
		}
		return map;
	}

	public void saveStudentUser(StudentUser user) {
		getSqlSessionTemplate().insert("User.insertStudentUser", user);
	}

	public void saveTeacherUser(TeacherUser user) {
		getSqlSessionTemplate().insert("User.insertTeacherUser", user);
	}
}
